package com.example.boxueguapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.boxueguapplication.model.User;

public class MyInfoActivity extends AppCompatActivity {

    private RelativeLayout mAppBar;
    private ImageView mBack;
    private TextView mTitleBar;
    private ImageView mUserImage;
    private TextView mUsername;
    private TextView mNike;
    private TextView mSex;
    private TextView mQianming;
    private RelativeLayout mNickBox;
    private RelativeLayout mQianmingBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        findView();
        mTitleBar.setText("个人资料");
        mBack.setOnClickListener(v -> finish());
        SharedPreferences userinfo = getSharedPreferences("userinfo", MODE_PRIVATE);
        String username = userinfo.getString("LoginUser", null);
        if (!TextUtils.isEmpty(username)){
            User user = User.find(User.class, "username = ?",username).get(0);
            mUsername.setText(user.getUsername());
            mUserImage.setBackgroundResource(user.getImage());
            mNike.setText(user.getNike());
            mSex.setText(user.getSex()==1?"男":"女");
            mQianming.setText(user.getQianming());
            mNickBox.setOnClickListener(v -> {
                Intent intent = new Intent(MyInfoActivity.this, ChangeInfoActivity.class);
                intent.putExtra("title","昵称");
                intent.putExtra("nike",user.getNike());
                startActivityForResult(intent,3);
            });
            mQianmingBox.setOnClickListener(view -> {
                Intent intent = new Intent(MyInfoActivity.this, ChangeInfoActivity.class);
                intent.putExtra("title","签名");
                intent.putExtra("qianming",user.getQianming());
                startActivityForResult(intent,4);
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==resultCode&&requestCode==3)
            mNike.setText(data.getStringExtra("Change"));
        if (requestCode==resultCode&&requestCode==4)
            mQianming.setText(data.getStringExtra("Change"));
    }

    private void findView() {
        mAppBar = findViewById(R.id.app_bar);
        mBack = findViewById(R.id.back);
        mTitleBar = findViewById(R.id.title_bar);
        mUserImage = findViewById(R.id.user_image);
        mUsername = findViewById(R.id.username);
        mNike = findViewById(R.id.Change);
        mSex = findViewById(R.id.sex);
        mQianming = findViewById(R.id.qianming);
        mNickBox = findViewById(R.id.nick_box);
        mQianmingBox = findViewById(R.id.qianming_box);
    }
}